// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.util;

import java.io.OutputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

public class ByteBufferOutputStream extends OutputStream {
    private ByteBuffer wrappedBuffer;
    private final boolean autoEnlarge;

    public ByteBufferOutputStream(ByteBuffer _wrappedBuffer, boolean _autoEnlarge) {
        this.wrappedBuffer = _wrappedBuffer;
        this.autoEnlarge = _autoEnlarge;
    }

    public ByteBuffer toByteBuffer() {
        ByteBuffer byteBuffer = this.wrappedBuffer.duplicate();
        byteBuffer.flip();
        return byteBuffer.asReadOnlyBuffer();
    }

    public ByteBuffer getWrappedBuffer() {
        return this.wrappedBuffer;
    }

    public void clear() {
        this.wrappedBuffer.clear();
    }

    public void flip() {
        this.wrappedBuffer.flip();
    }

    private void growTo(int int2) {
        int int0 = this.wrappedBuffer.capacity();
        int int1 = int0 << 1;
        if (int1 - int2 < 0) {
            int1 = int2;
        }

        if (int1 < 0) {
            if (int2 < 0) {
                throw new OutOfMemoryError();
            }

            int1 = Integer.MAX_VALUE;
        }

        ByteBuffer byteBuffer = this.wrappedBuffer;
        if (this.wrappedBuffer.isDirect()) {
            this.wrappedBuffer = ByteBuffer.allocateDirect(int1);
        } else {
            this.wrappedBuffer = ByteBuffer.allocate(int1);
        }

        byteBuffer.flip();
        this.wrappedBuffer.put(byteBuffer);
    }

    public void write(int bty) {
        try {
            this.wrappedBuffer.put((byte)bty);
        } catch (BufferOverflowException bufferOverflowException) {
            if (!this.autoEnlarge) {
                throw bufferOverflowException;
            }

            int _int = this.wrappedBuffer.capacity() * 2;
            this.growTo(_int);
            this.write(bty);
        }
    }

    public void write(byte[] _byte) {
        int int0 = 0;

        try {
            int0 = this.wrappedBuffer.position();
            this.wrappedBuffer.put(_byte);
        } catch (BufferOverflowException bufferOverflowException) {
            if (!this.autoEnlarge) {
                throw bufferOverflowException;
            }

            int int1 = Math.max(this.wrappedBuffer.capacity() * 2, int0 + _byte.length);
            this.growTo(int1);
            this.write(_byte);
        }
    }

    public void write(byte[] _byte, int int1, int int2) {
        int int0 = 0;

        try {
            int0 = this.wrappedBuffer.position();
            this.wrappedBuffer.put(_byte, int1, int2);
        } catch (BufferOverflowException bufferOverflowException) {
            if (!this.autoEnlarge) {
                throw bufferOverflowException;
            }

            int int3 = Math.max(this.wrappedBuffer.capacity() * 2, int0 + int2);
            this.growTo(int3);
            this.write(_byte, int1, int2);
        }
    }
}
